<style scoped>
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>修改数据项</legend>
        </fieldset>

        <form class="layui-form " >
            <input type="hidden" name="guid" placeholder="" autocomplete="off" :value="guid" class="layui-input">
            <div class="layui-form-item" >
                <label class="layui-form-label" >关联类型</label>
                <div class="layui-input-block" >
                    <select   id="relationtype"  name="relationtype"  lay-filter="relationtype" lay-verify="required"      lay-search="">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item" >
                <label class="layui-form-label" >采收批次编号</label>
                <div class="layui-input-block" >
                    <select   id="linkedguid"  name="linkedguid"  lay-filter="linkedguid"       lay-search="">

                    </select>
                </div>
            </div>


            <div class="layui-form-item" >
                <label class="layui-form-label" >质检标准</label>
                <div class="layui-input-block" >
                    <select   id="qamainguid"  name="qamainguid"  lay-filter="qamainguid" lay-verify="required"      lay-search="">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
            <div style="height: 500px"></div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'QaitemsEdit',
        props: ['id'],
        data () {
            return {
                guid : this.id,
                adata: {},
                qaassignment:{},
                typeEmnu:[],
                costincomeEmnu:[]
            }
        },
        mounted() {
            let that = this;
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                let loader = layer.load();

                form.on('select(relationtype)', function(data){
                    var relationtype = data.value;
                    if(relationtype != '1'){
                        $("#linkedguid").val("");
                        $("#linkedguid").attr("disabled","disabled");
                    }else{
                        $("#linkedguid").removeAttr("disabled"); //去除readonly属性
                    }
                    form.render('select');
                });



                //获取回显数据
                $.ajax({
                    url: '/trace/Qaassignment!listVUEPC.action',
                    data: {guid: that.guid},
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        console.log(res);
                        if (res.info != "error") {
                            that.qaassignment = res.info.rows[0];
                            if(that.qaassignment.relationtype   != '1'){
                                $("#linkedguid").val("");
                                $("#linkedguid").attr("disabled","disabled");

                            }
                            form.render('select');
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });

              //  $("#relationtype").attr("disabled","disabled");

                $.ajax({
                    url: '/trace/Qaassignment!getBase.action',
                    data: {},
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != "error") {
                            // $.each(res.dict, function (index, item) {
                            //     if(item.dictkey  == that.qaassignment.relationtype){
                            //         $('#relationtype').append("<option selected  value=" +item.dictkey +  " > " + item.dicttext  + "</option>" );
                            //     }else{
                            //         $('#relationtype').append("<option   value="  +item.dictkey +  " >" + item.dicttext    + "</option>" );
                            //     }
                            // });

                            $.each(res.dict, function (index, item) {
                                var isAddoption = true;
                                $.each(res.qaassignment, function (subindex, subitem) {
                                    if(item.dictkey == subitem.relationtype && item.dictkey != 1){
                                        isAddoption = false;
                                    }
                                });
                                if(item.dictkey  == that.qaassignment.relationtype && item.dictkey != 1){
                                    $('#relationtype').append("<option selected  value=" +item.dictkey +  " > " + item.dicttext  + "</option>" );
                                }
                                if(isAddoption){
                                        $('#relationtype').append("<option   value="  +item.dictkey +  " >" + item.dicttext    + "</option>" );
                                }
                            });
                            $('#linkedguid').append("<option   value="  +that.qaassignment.linkedguid +  " >" + that.qaassignment.batchname    + "</option>" );
                            $.each(res.procurementbatch, function (index, item) {
                                    $('#linkedguid').append("<option   value="  +item.guid +  " >" + item.name    + "</option>" );

                            });
                            $.each(res.qamain, function (index, item) {
                                if(item.guid  == that.qaassignment.qamainguid){
                                    $('#qamainguid').append("<option selected  value=" +item.guid +  " > " + item.qaname  + "</option>" );
                                }else{
                                    $('#qamainguid').append("<option   value="  +item.guid +  " >" + item.qaname    + "</option>" );
                                }
                            });

                            form.render('select');
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });


                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    console.log(data);



                    $.ajax({
                        url: '/trace/Qaassignment!update.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "success") {
                                that.toast("修改成功");
                                that.$parent.switchPage('Qaassignment');
                            } else{
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });

        },
        methods: {
            back: function () {
                this.$parent.switchPage(this.$parent.refView);
            },
            getqamain: function (){

            }

        }
    }
</script>
